<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    *{
        padding: 0;
        margin: 0;
    }
    div{
        height: 400px;
        margin: 50px auto;
    }
    div p{
        text-align: center;
        margin: 20px 0;
    }
</style>
<body>
    <div>
       <p> <input type="text"> <span>输入数字求和，数字之间用半角","号分隔</span></p>
      <p> <button>求和</button>&emsp; <span class="sp"></span></p>
    </div>
</body>
</html>
<script>
        var btn=document.querySelector('button')
        var input=document.querySelector('input')
        var sp=document.querySelector('.sp')
        // btn.onkeyup=function(){
        //     this.value = this.value.replace(/[^(\d)|(,)]/,"");
        // }
        btn.onclick=function(){
            if(input.value == ''){
                alert('不能为空')
            }
            var arr=input.value.split(',')
            var sum=0
           arr.forEach(item=>{
               sum+=parseInt(item)
           })
           sp.innerHTML=sum
    //    let num= arr.reduce(function(a,b){
    //         return a+Number(b)
    //     },0)
    //     sp.innerHTML+=num
        }
        var arr1='1.2&3.4&5.5'
        let a= arr1.split('.')
        console.log(a);
</script>